mui.init({
	keyEventBind: {
		backbutton: false,  //Boolean(默认true)关闭back按键监听,防止回到登录页面
	},
});
var phone = document.getElementById("phone"),
	headImg = document.getElementsByClassName("head-img")[0];

mui.plusReady(function() {
	//从storage中取出字符串并将字符串转化为对象
	var userData = JSON.parse(plus.storage.getItem("userinfo"));
	//由于app入口为首页，做判断是否登录，未登录则跳转登录，此时没有本地数据或数据仍需更新，故应在登录后触发自定义事件，拉取数据
	if(userData) {
		var mw = plus.webview.currentWebview();
		mui.fire(mw, 'loadMyPage', {})
	}
	
	//自定义事件，加载我的页面数据
	window.addEventListener('loadMyPage', function() {
		//信息填充
		document.getElementById("username").innerHTML = userData.username;
		document.getElementById("departmentname").innerHTML = "科室：" + userData.departmentname;
		phone.innerHTML = userData['phone'];
		//	document.getElementsByClassName("head-img")[0].src = "http://app.xiaoweicanting.com/" + userData.headimg;
		//头像，以下为从本地文件中获取
		if(mui.os.plus) {
			plus.io.resolveLocalFileSystemURL("_doc/head.jpg", function(entry) {
				var s = entry.fullPath;
				headImg.src = s;
			}, function(e) {
				headImg.src = '../../images/logo.png';
			})
		} else {
			headImg.src = '../../images/logo.png';
		}

		//跳转页面
		[].slice.call( document.querySelectorAll('*[data-url]') ).forEach(function(e) {
			e.addEventListener("tap", function() {
				var url = this.getAttribute("data-url");
				var re = /\w+[.]\w+/g;
				var id = re.exec(url);
				mui.openWindow(url, id, {});
			});
		});
	})

	//添加自定义事件，用户修改信息后，更新信息
	window.addEventListener('updateData', function(e) {
		//从storage中取出字符串并将字符串转化为对象
		var userData = JSON.parse(plus.storage.getItem("userinfo"));
		var targetDom,
			targetStr = e.detail.target;
		if(targetStr === 'headImg') {
			if(mui.os.plus) {
				plus.io.resolveLocalFileSystemURL("_doc/head.jpg", function(entry) {
					var s = entry.fullPath + "?version=" + new Date().getTime();
					headImg.src = s;
				}, function(e) {
					headImg.src = '../../images/logo.png';
				})
			} else {
				headImg.src = '../../images/logo.png';
			}
		} else {
			switch(targetStr) {
				case "account":
					targetDom = account;
					break;
				case "phone":
					targetDom = phone;
					break;
				case "email":
					targetDom = email;
					break;
				default:
					console.log("updateData溢出");
					break;
			}
			targetDom.innerHTML = userData[targetStr];
		}
	})
})